package com.vp.org.web.servlet;

import com.vp.org.dao.OrdersDao;
import com.vp.org.dao.impl.OrdersDaoImpl;
import com.vp.org.pojo.BookInfo;
import com.vp.org.pojo.Orders;
import com.vp.org.service.BookService;
import com.vp.org.service.OrderService;
import com.vp.org.service.impl.BookServiceImpl;
import com.vp.org.service.impl.OrderServiceImpl;
import com.vp.org.web.base.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "order", value = "/order")
public class OrderServlet extends BaseServlet {

    private OrderService orderService = new OrderServiceImpl();

    private BookService bookService = new BookServiceImpl();

    private OrdersDao ordersDao = new OrdersDaoImpl();

    public String query(HttpServletRequest req, HttpServletResponse resp) {

        int id = Integer.parseInt(req.getParameter("id"));

        List<Orders> orders = orderService.findOrdersByUserId(id);

        List<BookInfo> books = new ArrayList<>();

        for (Orders order : orders) {
            books.add(bookService.findByName(order.getGoodsName()));
        }

        String str = req.getContextPath() + "user/center/";

        req.getSession().setAttribute("orders", orders);
        req.getSession().setAttribute("books", books);

        return "r:"+ str +"user_1.jsp?page=1";
    }

    public String delete(HttpServletRequest req, HttpServletResponse resp) {

        int id = Integer.parseInt(req.getParameter("id"));

        Orders orders = new Orders();
        orders.setId(id);

        ordersDao.deleteOrders(orders);

        String str = req.getContextPath() + "user/center/";

        return "r:"+ str +"user_1.jsp?page=1&msg=1";
    }
}